﻿using System;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Text;
using System.Collections.Generic;
using NUnit.Framework;
using ScrappyDB;
using ScrappyDB.BaseClasses;

namespace ScrappyDB.IntegrationTests
{
    [TestFixture]
    public class LocalContextStoreTest
    {
        [TestFixtureSetUp]
        public  void Init()
        {
            var db = new Db();
            var newFoo = new Foo2
            {
                ID = 1,
                Bar = "phat"
            };
            db.SaveChanges(newFoo);

            var newFoo2 = new Foo2
            {
                ID = 2,
                Bar = "blah"
            };
            db.SaveChanges(newFoo2);

            var newFoo3 = new Foo2
            {
                ID = 3,
                Bar = "the quick brown fox"
            };
            db.SaveChanges(newFoo3);
        }

        [TestFixtureTearDown]
        public  void MyClassCleanup()
        {
            var db = new Db();
            var foos = db.Query<Foo2>();
            db.Delete<Foo2>(foos);
        }

        [Test]
        public void VerifyLocalContextTest()
        {
            var context = new Foo2Context();
            var x =  context.Foos.ToList();

            Assert.AreEqual(3,x.Count);

            //now delete all the data
            MyClassCleanup();

            //prove we still have the data locally
            Assert.AreEqual(3, x.Count);

            //run a new query
            x = (from a in context.Foos where a.ID == 1 select a).ToList();

            //prove the data is gone
            Assert.AreEqual(0, x.Count);

        }

      
    }

    public class Foo2 
    {
        [Key]
        public int ID { get; set; }
        public string Bar { get; set; }
    }

    public class Foo2Context : ScrappyDB.Linq.SdbContext
    {
        public ScrappyDB.Linq.SdbSet<Foo2> Foos { get; set; }
    }
}
